package com.yiwei.zxp.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zxp.BussinessMainActivity;
import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.ForgetPassActivity;
import com.yiwei.zxp.activity.LoginNewActivity;
import com.yiwei.zxp.activity.RegisterActivity;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.LoginBean;
import com.yiwei.zxp.constant.UserInfoGlobal;
import com.yiwei.zxp.util.MD5;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-5-17.
 */

public class LoginPassFragment extends BaseFragment {
    @BindView(R.id.edit_account)
    EditText editAccount;
    @BindView(R.id.btn_clear)
    ImageView btnClear;
    @BindView(R.id.edit_pass)
    EditText editPass;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.btn_forget)
    TextView btnForget;
    @BindView(R.id.btn_register)
    TextView btnRegister;
    @BindView(R.id.btn_pass_visible)
    ImageView btnPassVisible;
    private boolean isVisible = false;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_login_pass;
    }

    @Override
    protected void initView() {
        editAccount.setText(UserInfoGlobal.getPhone());
    }

    @OnTextChanged({R.id.edit_account, R.id.edit_pass})
    void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(UI.toString(editAccount)) && !TextUtils.isEmpty(UI.toString(editPass))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }



    @OnClick({R.id.btn_clear, R.id.btn_submit, R.id.btn_forget, R.id.btn_register, R.id.btn_pass_visible})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_clear:
                editAccount.setText("");
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editAccount))) {
                    ToastUtil.makeText(getContext(), "手机号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editPass))) {
                    ToastUtil.makeText(getContext(), "密码不能为空");
                    return;
                }
                ((LoginNewActivity) getActivity()).getLogin(UI.toString(editAccount), UI.toString(editPass));
                break;
            case R.id.btn_forget:
                Util.startActivity(getActivity(), ForgetPassActivity.class);
                break;
            case R.id.btn_register:
                Util.startActivity(getActivity(), RegisterActivity.class);
                break;
            case R.id.btn_pass_visible:
                if (!isVisible) {
                    //如果选中，显示密码
                    editPass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    btnPassVisible.setImageResource(R.mipmap.img_eyes_open);
                } else {
                    //否则隐藏密码
                    editPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    btnPassVisible.setImageResource(R.mipmap.img_eyes_close);
                }
                isVisible = !isVisible;
                break;

        }
    }
}
